package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.entity.SysProject;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.SysUserProject;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.mapper.SysProjectMapper;
import com.ruoyi.system.mapper.SysUserProjectMapper;
import com.ruoyi.system.service.ISysProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 项目管理Service业务层处理
 * 
 * @author sux
 * @date 2021-09-03
 */
@Service
public class SysProjectServiceImpl implements ISysProjectService
{
    @Autowired
    private SysProjectMapper sysProjectMapper;

    @Autowired
    private SysUserProjectMapper userProjectMapper;

    /**
     * 查询项目管理
     * 
     * @param projectId 项目管理ID
     * @return 项目管理
     */
    @Override
    public SysProject selectSysProjectById(Long projectId)
    {
        return sysProjectMapper.selectSysProjectById(projectId);
    }

    /**
     * 查询项目管理列表
     * 
     * @param sysProject 项目管理
     * @return 项目管理
     */
    @Override
    public List<SysProject> selectSysProjectList(SysProject sysProject)
    {
        return sysProjectMapper.selectSysProjectList(sysProject);
    }

    /**
     * 新增项目管理
     * 
     * @param sysProject 项目管理
     * @return 结果
     */
    @Override
    public int insertSysProject(SysProject sysProject)
    {
        sysProject.setCreateTime(DateUtils.getNowDate());
        return sysProjectMapper.insertSysProject(sysProject);
    }

    /**
     * 修改项目管理
     * 
     * @param sysProject 项目管理
     * @return 结果
     */
    @Override
    public int updateSysProject(SysProject sysProject)
    {
        sysProject.setUpdateTime(DateUtils.getNowDate());
        return sysProjectMapper.updateSysProject(sysProject);
    }

    /**
     * 删除项目管理对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteSysProjectByIds(String ids)
    {
        return sysProjectMapper.deleteSysProjectByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除项目管理信息
     * 
     * @param projectId 项目管理ID
     * @return 结果
     */
    @Override
    public int deleteSysProjectById(Long projectId)
    {
        return sysProjectMapper.deleteSysProjectById(projectId);
    }

    @Override
    public List<SysProject> selectProjectsByUserId(Long userId) {
        List<SysProject> userProjects = sysProjectMapper.selectProjectsByUserId(userId);
        List<SysProject> projects = selectProjectAll();
        for (SysProject project : projects)
        {
            for (SysProject userProject : userProjects)
            {
                if (project.getProjectId().longValue() == userProject.getProjectId().longValue())
                {
                    project.setFlag(true);
                    break;
                }
            }
        }
        return projects;
    }

    @Override
    public List<SysProject> selectProjectAll() {
        return this.selectSysProjectList(new SysProject());
    }

    @Override
    public int authDataScope(SysProject project) {
        return 0;
    }

    @Override
    public String checkProjectNameUnique(SysProject project) {
        return null;
    }

    @Override
    public String checkProjectKeyUnique(SysProject project) {
        return null;
    }

    @Override
    public void checkProjectAllowed(SysProject project) {

    }

    @Override
    public int countUserProjectByProjectId(Long projectId) {
        return 0;
    }


    @Override
    public int deleteAuthUser(SysUserProject userProject) {
        return userProjectMapper.deleteUserProjectInfo(userProject);
    }

    @Override
    public int deleteAuthUsers(Long projectId, String userIds) {
        return userProjectMapper.deleteUserProjectInfos(projectId, Convert.toLongArray(userIds));
    }

    @Override
    public int insertAuthUsers(Long projectId, String userIds) {
        Long[] users = Convert.toLongArray(userIds);
        // 新增用户与项目管理
        List<SysUserProject> list = new ArrayList();
        for (Long userId : users)
        {
            SysUserProject userProject = new SysUserProject();
            userProject.setUserId(userId);
            userProject.setProjectId(projectId);
            list.add(userProject);
        }
        return userProjectMapper.batchUserProject(list);
    }

    @Override
    public List<SysUserProject> selectUserProjectsByUserId(Long userId) {

        return userProjectMapper.selectUserProjectsByUserId(userId);
    }

    @Override
    public List<SysUserProject> selectAllUserProjects(Long userId) {

        return userProjectMapper.selectAllUserProjects(userId);
    }

//    @Override
//    public void checkUserAllowed(SysProject project) {
//
//    }
    @Override
    public int changeStatus(SysProject project) {
        return sysProjectMapper.updateSysProject(project);
    }

}
